package com.invoicing.controller;


import com.invoicing.MainController;
import com.invoicing.Service.TypeService;
import com.invoicing.Service.UserService;
import com.invoicing.view.MenuView;
import com.invoicing.view.RegisterView;

import javafx.fxml.FXML;
import de.felixroske.jfxsupport.FXMLController;
import com.invoicing.entity.User;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import java.io.*;
import java.net.ConnectException;
import java.net.URL;
import java.util.ResourceBundle;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class LoginController implements Initializable {


    @Autowired
    private UserService userService;
    @Autowired
    private TypeService typeService;


    @FXML
    private TextField name;

    @FXML
    private PasswordField password;

    @FXML
    private Button login;

    @FXML
    private Button register;

    @FXML
    private CheckBox showPassword;

    @FXML
    private TextField password_text;

    @FXML
    private CheckBox remember;


    @FXML
    private void setLogin() {


        Alert alert_login = new Alert(Alert.AlertType.ERROR);
        if (name.getText().length() == 0) {
            alert_login.setTitle("提示");
            alert_login.setContentText("用户名不能为空！");
            alert_login.show();

        }
        else if(name.getText().length()>20){
            alert_login.setTitle("提示");
            alert_login.setContentText("用户名过长！不得超过20个字符");
            alert_login.show();
            name.clear();
        }
        else {
            if (password.getText().length() == 0) {
                alert_login.setTitle("提示");
                alert_login.setContentText("密码不能为空！");
                alert_login.show();
            } else {
                if (!userService.userExit(name.getText())) {
                    alert_login.setTitle("提示");
                    alert_login.setContentText("该账户不存在！");
                    alert_login.show();
                    password.clear();
                } else {
                    if (userService.passwordConform(name.getText(), password.getText())) {
                        MainController.showView(MenuView.class);

                    } else {
                        alert_login.setTitle("提示");
                        alert_login.setContentText("密码错误！");
                        alert_login.show();
                        password.clear();
                    }
                }
            }
        }
    }

    @FXML
    private void register() {
        Alert alert_register = new Alert(Alert.AlertType.ERROR);
        if (userService.getCount() >= 1) {
            alert_register.setTitle("提示");
            alert_register.setContentText("系统已有账户，请登录");
            alert_register.show();
        } else {
            MainController.showView(RegisterView.class);
        }

    }

    @FXML
    private void showPassword() {
        if (showPassword.isSelected()) {
            password_text.setText(password.getText());
            password_text.setVisible(true);
            password.setVisible(false);
            return;
        }
        password.setText(password_text.getText());
        password.setVisible(true);
        password_text.setVisible(false);
    }

    @FXML
    private void setRemember() {
        if (remember.isSelected()) {
            try {
                FileOutputStream oFile = new FileOutputStream("tmpuser.properties", false);
                oFile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            File f = new File("tmpuser.properties");
            f.delete();
        }

    }

    /**
     * Called to initialize a controller after its root element has been
     * completely processed.
     *
     * @param location  The location used to resolve relative paths for the root object, or
     *                  <tt>null</tt> if the location is not known.
     * @param resources The resources used to localize the root object, or <tt>null</tt> if
     */
    public void initialize(URL location, ResourceBundle resources) {
            if (new File("tmpuser.properties").exists()) {
                try{
                    if (userService.getRecent() != null) {
                        remember.setSelected(true);
                        User user = userService.getRecent();
                        name.setText(user.getName());
                        password.setText(user.getPassword());
                    }
                }catch (Exception e){

                }


            }




    }
}
